package com.mijie.homi.search.util;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

//import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;

public class tonitsiTool {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 String split1="-";
		 String  split2="\t";
		 String encode1="utf-8";
		 String encode2="utf-8";
		 
		 tonitsiTool c=new tonitsiTool();
		 
		 String path1="C:\\Users\\Administrator\\Desktop\\tonitsi\\tonitsi.txt";
		 String path2="C:\\Users\\Administrator\\Desktop\\tonitsi\\1伪原创同义词库(13000条).txt";
		 String target="c:\\word\\tonitsi1_deduli_deFei_join_ca25.txt";
		 
		 
		  path1="c:\\word\\tonitsi1_deduli_deFei.txt";
		  
		  
	 	//  c.join(path1,target);

	//  deduli(path1);
		// String s="幸灾乐祸,同病相怜";
		// System.out.println(listUtil.fejwe(s));
	//	  deduli4feitsi();
	// 	  showTonitsiJondFeitsi();
		//  deFeiwordFromTonitsilib();
		  
		  String path_reva="c:\\word\\toni_reva.txt";
		  String path_reva_split="c:\\word\\toni_reva_split.txt";
		//	c.reva_split(path_reva,path_reva_split);
		  c.joinFromDB("c:\\word\\tonitsi1_deduli_deFei_joinCa25_1800.txt");
		 System.out.println("---f");
	}

	/**
	 * line id
	 */
	Set<String> looked = new HashSet<String>();
	Set<String> lookedWord = new HashSet<String>();
	List<String> li_wait_word = new ArrayList<String>();

	private void joinFromDB(String target) {
		List<Map> li = getTableFromdb();
		List<String> li_r = new ArrayList<String>();
		for (Map mp : li) {
			String id = (String) mp.get("id");
			if (looked.contains(id))
				continue;
			String line = (String) mp.get("words");
			String line_tonitsi = getTonitsiBydb(line);

			li_r.add(line_tonitsi);
			looked.add(id);

		}
		fileC0 fc = new fileC0();
		fc.saveList2file(li_r, target);

	}

	private String getTonitsiBydb(String line) {
		String[] a=line.split(" ");
		for(String word:a)
		{
			
			if(lookedWord.contains(word))
				continue;
			
		//	String tonitsi=
			
			lookedWord.add(word);
		}
		return null;
	}

	private List<Map> getTableFromdb() {
		List<Map> li=new ArrayList<Map>();
		String sql="select * from 同义词词库_原始格式  order by id ";
		Dbcontroller c=new DbNdsController();
		ResultSet rs=	c.getrs(sql);
		try {
			while (rs.next()) {

				// 首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
				// 然后使用GB2312字符集解码指定的字节数组
				// name = new String(name.getBytes("ISO-8859-1"),"GB2312");

				// 输出结果
				String id = rs.getString("id");
				String words = rs.getString("words");
				Map mp=new HashMap<String, String>();
		 
				mp.put("id", id);
			 
				mp.put("words", words);
		 
				 
				 
					 li.add(mp);
					 
			 
			}
		} catch (SQLException e) {
			e.printStackTrace();
			 throw new RuntimeException(e);			
		}
		c.close();
		return li;
	}

	private void reva_split(String pathReva, String pathRevaSplit) {
		listUtil luc=new listUtil();
		List<String> li_r=luc.fileRead2list(pathReva, "gbk");
		
		List<String> li_r2=new ArrayList<String>();
		for(String line:li_r)
		{
//			String[] a=line.split("\t");
//			String w1=a[0];
//			if(w1.length()>2)
//			{
			String w1=line.replaceAll(" ", "");
			
			w1=w1.replaceAll("", "");
			w1=w1.replaceAll("", "");
			  w1=w1.replaceAll("\t", "");
			  w1=w1.trim();
			
				List<String> w1_process=strUtil.splitByLen(w1,2);
				String s=listUtil.toString(w1_process, "|");
				li_r2.add(s);
			//}
		}
		
		fileC0 fc=new fileC0();
		fc.saveList2file(li_r2, pathRevaSplit);
		
	}

	private static void deFeiwordFromTonitsilib() {
		List<String> li_r=new ArrayList<String>();
		 String tonitsi="c:\\tonitsi1_deduli.txt";
		 String paichu="c:\\paichu.txt";
		 tonitsiTool tt=new tonitsiTool();
			List<String> paichuLi =tt.getListXdirectWord(paichu);
			Set<String> paichuSet=listUtil.list2set(paichuLi);
			
			fileC0 fc = new fileC0();
			List<String> li1=fc.fileRead2list(tonitsi);
			int n=0;
			for(String str:li1)
			{
				n++;
				if (n % 300 == 0)
					System.out.println("---  nowing...:" + n);
				if(paichuSet.contains(str))
					continue;
				if(str.trim().length()==0)
					continue;
				li_r.add(str);
			}
			
			fc.saveList2file(li_r, "c:\\tonitsi1_deduli_deFei.txt");
		 
		
	}

	@SuppressWarnings("unchecked")
	private static void showTonitsiJondFeitsi() {
		 String tonitsi="c:\\tonitsi1_deduli_deFei.txt";
		 String feitsi="c:\\feitsi_deduli.txt";
			fileC0 fc = new fileC0();
			Set set=fc.fileRead2Set(tonitsi);
			
			
			tonitsiTool tt=new tonitsiTool();
			List<String> li1 =tt.getListXdirectWord(feitsi);
			for(String str:li1)
			{
				if(set.contains(str))
					System.out.println(str);
			}
			
			
		
	}

	private List<String> getListXdirectWord(String path1) {
		fileC0 fc = new fileC0();
		List<String> li1 = fc.fileRead2list(path1);
		
		List<String> li2=new ArrayList<String>();
		for(String str:li1)
		{
			String strX=listUtil.fejwe(str);
			li2.add(strX);
			
		}
		
		li1.addAll(li2);
		return li1;
	}

	private void join(String path1, String target) {
		List<String> li_r=new ArrayList<String>();
		fileC0 fc = new fileC0();
		List<String> li1 = fc.fileRead2list(path1);
	 
	 	int n = 0;
		for (String line : li1) {
			n++;
			if (n % 300 == 0)
				System.out.println("---join nowing...:" + n);
			if(this.lookedLines.contains(n))
				continue;
		 if(line.length()==0)
			 continue;
		 
		 
			String tonitsi=getTonitsi(line,li1);
//            if(tonitsiTool!=null)
//            	linex=linex+","+tonitsi;
			String line2=tonitsi;
            li_r.add(line2);
		}
		
		fc.saveList2file(li_r, target);
		
		
	}

	private static void deduli(String path1) {
		fileC0 fc = new fileC0();
		List<String> li1 = fc.fileRead2list(path1);
		li1=	listUtil.deDulicate(li1);
		li1=	listUtil.deDulicateXing(li1);
		
		tonitsiTool c=new tonitsiTool();
		List<String> li2=c.clearLenMini(li1,2);
		fc.saveList2file(li2, "c:\\tonitsi1_deduli.txt");
		
	}
	
	private static void deduli4feitsi() {
		String path1="C:\\Users\\Administrator\\Desktop\\tonitsi\\feitsi.txt";
		fileC0 fc = new fileC0();
		List<String> li1 = fc.fileRead2list(path1);
		li1=	listUtil.deDulicate(li1);
		li1=	listUtil.deDulicateXing(li1);
		
		tonitsiTool c=new tonitsiTool();
		List<String> li2=c.clearLenMini(li1,2);
		fc.saveList2file(li2, "c:\\feitsi_deduli.txt");
		
	}

	private List<String> clearLenMini(List<String> li1, int miniLen) {
		List<String> li_r=new ArrayList<String>();
		for(String str : li1){
			str=str.trim();
			if(str.length()<3)
				continue;
			li_r.add(str);
		}
		return li_r;
	}
Set<Integer> lookedLines=new HashSet<Integer>();
	private void join(String path1, String split1, String encode1,
			String path2, String split2, String encode2, String target) {
		List<String> li_r=new ArrayList<String>();
		fileC0 fc = new fileC0();
		List<String> li1 = fc.fileRead2list(path1);
		List<String> li2 = fc.fileRead2list(path2);	
		li2=changeListLineSplitor(li2);
		int n = 0;
		for (String line : li1) {
			n++;
			if (n % 300 == 0)
				System.out.println("---join nowing...:" + n);
			if(lookedLines.contains(n))
				continue;
			
			line=line.trim();
			String linex=line.replaceAll(split1, ",");
			String tonitsi=getTonitsi(linex,li2);
//            if(tonitsiTool!=null)
//            	linex=linex+","+tonitsi;
            li_r.add(linex);
		}
		
		fc.saveList2file(li_r, target);
		
	}

	private String getTonitsi(String line, List<String> li2) {
			String[] a=line.split(",");
			String tonitsi="";
			for(String word:a)
			{
				  tonitsi=tonitsi+","+getTonitsi4wordByLucene(word);			
				
			}
			tonitsi=strUtil.trimx(",", tonitsi) ;
			tonitsi=listUtil.deDulicate(tonitsi);
			
		 
			 
			
		return tonitsi;
	}
	LuceneSearch LuceneSearchor;
	private String getTonitsi4wordByLucene(String word) {
		if(LuceneSearchor==null)
		LuceneSearchor = new LuceneSearch();
		String s=LuceneSearchor.search4tonitsi(word);
		s=strUtil.trimx(",", s) ;
		
		 this.lookedLines.addAll( LuceneSearchor.lookSet);
		return s;
	}

	private String getTonitsi4word(String word, List<String> li2) {
	String	tonitsi="";
		
		for (int i=0;i<li2.size();i++)  {
			String linex=li2.get(i);
			if(isContain(linex,word))
			{				
				tonitsi=tonitsi+","+linex;
				li2.set(i, "");
			}
			
		}
		tonitsi=strUtil.trimx(",",tonitsi);
		return tonitsi;
	}

	private boolean isContain(String linex, String word) {
		 Set set=strUtil.toSet(linex);
		 if(set.contains(word))
			 return true;
		 else
		return false;
	}

	private List<String> changeListLineSplitor(List<String> li2) {
		List<String> li_r=new ArrayList<String>();
		for (String line : li2) {
			line=line.trim();
			String linex=line.replaceAll("\t", ",");
			li_r.add(linex);
		}
		return li_r;
	}

}
